package JZOffer;


import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

// https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/?envType=study-plan&id=lcof&plan=lcof&plan_progress=xxj1ozje
// 用两个栈实现队列
public class Day001_1 {
    static class CQueue {
        private Stack<Integer> stack1 = null;
        private Stack<Integer> stack2 = null;
        public CQueue() {
            stack1 = new Stack<>();
            stack2 = new Stack<>();
        }

        public void appendTail(int value) {
            stack1.push(value);
        }

        public int deleteHead() {
            if (stack2.isEmpty()) {
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
                if (stack2.isEmpty()) {
                    return -1;
                }
            }
            return stack2.pop();
        }
    }

    // 使用更快的 Deque
    static class CQueue2 {
        private Deque<Integer> stack1 = null;
        private Deque<Integer> stack2 = null;
        public CQueue2() {
            stack1 = new ArrayDeque<>();
            stack2 = new ArrayDeque<>();
        }

        public void appendTail(int value) {
            stack1.push(value);
        }

        public int deleteHead() {
            if (stack2.isEmpty()) {
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
                if (stack2.isEmpty()) {
                    return -1;
                }
            }
            return stack2.pop();
        }
    }
}
